package com.starbaba.stat.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.starbaba.core.service.CommonService;
import com.starbaba.stat.model.BusiCircleVO;
import com.starbaba.stat.model.RegionVO;

/**
 * 城市区域缓存
 * 
 * @author xmiles
 *
 */
public class RegionInfoHandler {

	private static RegionInfoHandler instance;
	private List<RegionVO> privonceList;
	private HashMap<String, List<RegionVO>> cityList = new HashMap<String, List<RegionVO>>();
	private HashMap<String, List<RegionVO>> areaList = new HashMap<String, List<RegionVO>>();
	private HashMap<String, List<BusiCircleVO>> circleList = new HashMap<String, List<BusiCircleVO>>();
	/**
	 * 缓存Code与区域的数据，通过Code快速找到城市，区域
	 */
	private HashMap<String, RegionVO> regionMap = new HashMap<String, RegionVO>();
	/**
	 * 缓存Code与商圈的数据，通过Code快速找到商圈
	 */
	private HashMap<Long, BusiCircleVO> circleMap = new HashMap<Long, BusiCircleVO>();

	private RegionInfoHandler() {
	}

	public static RegionInfoHandler getInstance() {
		if (instance == null) {
			instance = new RegionInfoHandler();
		}
		return instance;
	}

	@SuppressWarnings("unchecked")
	public List<RegionVO> getPrivonce(CommonService service) {
		if (privonceList == null) {
			privonceList = new ArrayList<RegionVO>();
			privonceList.add(new RegionVO("-1", "未知"));
			privonceList.addAll((List<RegionVO>) service
					.doFindByHQL("From RegionVO where level=1 order by pinyin"));
//			privonceList = (List<RegionVO>) service
//					.doFindByHQL("From RegionVO where level=1");

			for (RegionVO regionVO : privonceList) {
				regionMap.put(regionVO.getId(), regionVO);
			}
		}
		return privonceList;
	}

	@SuppressWarnings("unchecked")
	public List<RegionVO> getCityFromPrivonce(CommonService service,
			String privonceId) {
		if (cityList.get(privonceId) == null) {
			if ("-1".equals(privonceId)) {
				List<RegionVO> list =  new ArrayList<RegionVO>();
				list.add(new RegionVO("-1", "未知"));
				cityList.put(privonceId, list);
			} else {
				List<RegionVO> list = (List<RegionVO>) service.doFindByHQL(
						"From RegionVO where level=2 and pid=? order by pinyin", privonceId);
				cityList.put(privonceId, list);
				for (RegionVO regionVO : list) {
					regionMap.put(regionVO.getId(), regionVO);
				}
			}
		}
		return cityList.get(privonceId);
	}

	@SuppressWarnings("unchecked")
	public List<RegionVO> getAreaFromCity(CommonService service, String cityId) {
		if (areaList.get(cityId) == null) {
			List<RegionVO> list = (List<RegionVO>) service.doFindByHQL(
					"From RegionVO where level=3 and pid=? order by pinyin", cityId);
			areaList.put(cityId, list);
			for (RegionVO regionVO : list) {
				regionMap.put(regionVO.getId(), regionVO);
			}
		}
		return areaList.get(cityId);
	}

	public String getRegionName(CommonService service, String id) {
		String name = "";
		if (!StringUtils.isBlank(id) && !id.equals("null")) {
			RegionVO regionVO = regionMap.get(id);
			if (regionVO == null) {
				RegionVO vo = (RegionVO) service.findUniqueByHQL(
						"From RegionVO where id=?", id);
				if (vo != null) {
					regionMap.put(vo.getId(), vo);
					name = regionMap.get(id).getName();
				} else {
					name = "";
				}
			} else {
				name = regionVO.getName();
			}
		}
		return name;
	}

	public RegionVO getRegionVo(CommonService service, String id) {
		RegionVO returnVo = null;
		if (!StringUtils.isBlank(id) && !id.equals("null")) {
			returnVo = regionMap.get(id);
			if (returnVo == null) {
				RegionVO vo = (RegionVO) service.findUniqueByHQL(
						"From RegionVO where id=? ", id);
				if (vo != null) {
					regionMap.put(vo.getId(), vo);
					returnVo = regionMap.get(id);
				} else {
					returnVo = null;
				}
			}
		}
		return returnVo;
	}
	
	
	public RegionVO getRegionVoFromName(CommonService service, String name) {
		RegionVO vo = (RegionVO) service.findUniqueByHQL(
				"From RegionVO where name=? and level = 1", name);
		return vo;
	}
	
	public RegionVO getCityVoFromName(CommonService service, String name) {
		RegionVO vo = (RegionVO) service.findUniqueByHQL(
				"From RegionVO where name=? and level = 2", name);
		return vo;
	}

	@SuppressWarnings("unchecked")
	public List<BusiCircleVO> getCircleFromArea(CommonService service,
			String cityId) {
		if (circleList.get(cityId) == null) {
			List<BusiCircleVO> list = (List<BusiCircleVO>) service.doFindByHQL(
					"From BusiCircleVO where areaid=? order by pinyin", cityId);
			circleList.put(cityId, list);
			for (BusiCircleVO regionVO : list) {
				circleMap.put(regionVO.getId(), regionVO);
			}
		}
		return circleList.get(cityId);
	}

	public String getCircleName(CommonService service, String id) {
		String name = "";
		if (!StringUtils.isBlank(id) && !id.equals("null")) {
			Long circleId = Long.valueOf(id);
			BusiCircleVO busiCircleVO = circleMap.get(circleId);
			if (busiCircleVO == null) {
				BusiCircleVO vo = (BusiCircleVO) service.findUniqueByHQL(
						"From BusiCircleVO where id=?", circleId);
				if (vo != null) {
					circleMap.put(vo.getId(), vo);
					name = circleMap.get(circleId).getCname();
				} else {
					name = "";
				}
			} else {
				name = busiCircleVO.getCname();
			}
		}
		return name;
	}

	public BusiCircleVO getCircleVo(CommonService service, String id) {
		BusiCircleVO returnVo = null;
		if (!StringUtils.isBlank(id) && !id.equals("null")) {
			Long circleId = Long.valueOf(id);
			returnVo = circleMap.get(circleId);
			if (returnVo == null) {
				BusiCircleVO vo = (BusiCircleVO) service.findUniqueByHQL(
						"From BusiCircleVO where id=?", circleId);
				if (vo != null) {
					circleMap.put(vo.getId(), vo);
					returnVo = circleMap.get(circleId);
				} else {
					returnVo = null;
				}
			}
		}
		return returnVo;
	}
}
